package yl.hs.bmipfull.service.laboratorydata.imple;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmStoneReportScreeningDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmStoneReportScreeningEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabRmStoneReportScreeningService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 原材检测报告——石子报告筛分信息服务层接口实现
 * @since 2022年06月24日 09时48分18秒 <br/>
 */
@Service
public class LabRmStoneReportScreeningServiceImpl extends LaboratorydataBaseService implements ILabRmStoneReportScreeningService {

    @Autowired
    ILabRmStoneReportScreeningDao dao;

    @Override
    public List<LabRmStoneReportScreeningEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabRmStoneReportScreeningEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<LabRmStoneReportScreeningEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmStoneReportScreeningEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmStoneReportScreeningEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public LabRmStoneReportScreeningEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabRmStoneReportScreening(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabRmStoneReportScreeningEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabRmStoneReportScreeningEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getReportSN(), "插入原材检测报告——石子报告筛分信息信息", Tl.toJson(entity), LabRmStoneReportScreeningEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新原材检测报告——石子报告筛分信息信息", Tl.toJson(entity), LabRmStoneReportScreeningEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmStoneReportScreeningEntity.class);
        }
        return result;
    }
}